//
//  HMEmoticonCollectionViewCell.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/10/6.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
//自定义表情视图的cell
class HMEmoticonCollectionViewCell: UICollectionViewCell {
    //记录当前cell的表情按钮
    lazy var emoticonButtonArray:[HMEmoticonButton] = [HMEmoticonButton]()
    
    //准备cell需要的数据源
    var emoticonArray:[HMEmoticon]? {
        didSet {
            guard let entArray = emoticonArray else {
                return
            }
            
            //执行到此说明数组不为空
            //先把表情按钮全部隐藏
            for button in emoticonButtonArray{
                button.isHidden = true
            }
            
            //遍历表情数组模型，给表情按钮绑定数据
            for (i,emoticon) in entArray.enumerated() {
                //获取表情按钮,每绑定一个数据显示一个按钮
                let button = emoticonButtonArray[i]
                //设置自定义表情按钮的模型
                button.emoticon = emoticon
//                button.isHidden = false
//                if emoticon.type == "0" {
//                    //代表图片，直接设置按钮的image
//                    button.setImage(UIImage(named:emoticon.path!), for: .normal)
//                    button.setTitle(nil, for: .normal)
//                }else {
//                    //代表emoji
//                    button.setTitle((emoticon.code! as NSString).emoji(), for: .normal)
//                    button.setTitle(nil, for: .normal)
//                    
//                }
                
            }
        }
    }
    
    var indexPath:IndexPath? {
        didSet {
            messageLabel.text = "当前显示的是第\(indexPath!.section + 1)组第\(indexPath!.item + 1)行"
        }
    }
    
    //懒加载控件
    fileprivate lazy var messageLabel:UILabel = {
        let label = UILabel(textColor:UIColor.red,fontSize:30)
        return label
    }()
    
    //删除表情按钮
    fileprivate lazy var deleteButton:UIButton = {
       let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(deleteButtonClick), for: .touchUpInside)
        button.setImage(UIImage(named:"compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        addChildEmoticonButton()
        contentView.addSubview(deleteButton)
//        contentView.addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
    }
    
    //删除按钮的点击事件
    @objc private func deleteButtonClick() {
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNotification), object: nil)
    }
    
    //添加表情按钮的方法
    func addChildEmoticonButton() {
        for _ in 0..<20 {
            let button = HMEmoticonButton()
            //添加点击事件
            button.addTarget(self, action: #selector(emoticonButtonClick(btn:)), for: .touchUpInside)
            //button.backgroundColor = RandomColor()
            //设置字体大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            //添加到ContentView上
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
            
        }
    }
    
    //表情按钮的点击事件
    @objc private func emoticonButtonClick(btn:HMEmoticonButton) {
        //取到自定义按钮的模型
        let emoticon = btn.emoticon
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoticonNotification), object: emoticon)
        let popView = HMEmoticonPopView.emoticonPopView()
        //设置popView的表情模型
        popView.emoticonButton.emoticon = emoticon
        //获取window
        let window = UIApplication.shared.windows.last
        window?.addSubview(popView)
        //把表情按钮在collectionView上的坐标转换到window上
        //let convertRect = contentView.convert(btn.frame, to: window)
        let convertRect = contentView.convert(btn.frame, to: window)
        
        //让popView的中心点等于表情按钮的水平中心点
        popView.centerX = convertRect.midX
        popView.y = convertRect.maxY - popView.height
        
        //延时0.25秒后移除
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { 
            popView.removeFromSuperview()
        }
        
    }
    
    //设置每个表情按钮的大小和位置
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //每个表情按钮的宽度和高度
        let itemWidth = width / 7
        let itemHeight = height / 3
        
        //设置删除按钮的大小和位置
        deleteButton.size = CGSize(width: itemWidth, height: itemHeight)
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemHeight
        
        //遍历表情按钮数组
        for (i,button) in emoticonButtonArray.enumerated() {
            button.size = CGSize(width: itemWidth, height: itemHeight)
            //计算行和列的索引
            let rowIndex = i / 7
            let colIndex = i % 7
            
            //设置x，y坐标
            button.x = CGFloat(colIndex) * itemWidth
            button.y = CGFloat(rowIndex) * itemHeight
            
            
            
            
        }
        
        
    }
}
